#include "MyString.h"
#include <cstring>
#include <iostream>


MyString::MyString():capacity(15),size(0)//无参构造
{    
    this->s = new char[this->capacity];
}

MyString::MyString(const char *str)//带参构造
{
    this->size = strlen(str);
    this->capacity = this->size + 1;
    this->s = new char[this->capacity];
    strcpy(this->s,str);

}

MyString::~MyString()
{
    delete []this->s;//this指向成员变量，写给我自己看的
}

MyString::MyString(const MyString &str)
{
    this->capacity = str.capacity;
    this->size = str.size;
    this->s = new char[this->capacity];
    strcpy(this->s,str.s);
}

MyString &MyString::operator=(const MyString &str)
{
    delete []this->s;
    this->capacity = str.capacity;
    this->size = str.size;
    this->s = new char[this->capacity];
    strcpy(this->s,str.s);
    return *this;
}

MyString &MyString::operator+=(const MyString &str)
{
    this->size += str.size;
    //空间不够
    if(this->size >= this->capacity)
    {
        this->capacity = this->size + 1;
        char* newP = new char[this->capacity];
        strcpy(newP,this->s);
        delete []this->s;
        this->s = newP;       
    }
    strcat(this->s,str.s);
    return *this;
}

MyString MyString::operator+(const MyString &str)
{
    MyString result(*this);//先拷贝自己当前对象进来
    result += str;//后面再进行运算加上str
    return result;
}

void MyString::operator+=(const char &c)
{
    if(this->size >= this->capacity - 1)//留\0的位置
    {
        this->capacity *= 2;
        char* newP = new char[this->capacity];
        strcpy(newP,this->s);
        delete []this->s;
        this->s = newP;       
    }
    this->s[this->size++]= c;//给一个加一个
    this->s[this->size] = '\0'; //在最末尾输入\0
}

MyString & MyString::operator-=(const MyString &str)
{
    char *dest = strstr(this->s,str.s);//匹配到相同字符串位置
    while(dest != nullptr)//匹配不到相同字符串的首字母时
    {
        char *back = dest + str.size;//匹配到字符的尾巴
        while(*back != '\0')//一直匹配到\0为止
        {
            *dest++ = *back++;
        }
        *dest = '\0';//给新的字符串添加\0
        this->size -= str.size;//减法之后的字符串长度
        dest = strstr(this->s,str.s);//继续进行匹配
    }    
    return *this;//返回现有的减法之后的字符串
}

MyString MyString::operator-(const MyString &str)//与加法类似
{
    MyString result(*this);//先传递当前对象进来
    result -= str;//后面再进行运算减去str
    return result;
}

char &MyString::operator[](int index)//返回的是字符所以用char，因为返回的是字符值，所以要引用
{
    return this->s[index];//返回下标所在位置的字符
}

bool MyString::operator==(const MyString &str)//==用bool类型返回,bool在c++是关键字
{
    return strcmp(this->s,str.s) == 0;//返回值为0 ，说明相等
}

bool MyString::operator!=(const MyString &str)
{
    return strcmp(this->s,str.s) != 0;//不能写==1，因为strcmp代表的是ascll码相减
}

bool MyString::operator&&(const MyString &str)
{
    return this->size != 0 && str.size != 0;//判断两个字符串都不为空
}

std::ostream &operator<<(std::ostream &os, const MyString &str)
{
    os<<str.s;
    return os;
}

std::istream &operator>>(std::istream &is, MyString &str)
{
    // //输入要先判断内存够不够大
    // char s[1024] = {0};//缓冲区默认是1024字节个数字0，输出字符为nul空字符
    // is>>s;//表示从输入流中读取一个字符，直到遇到空字符（'\0'）为止，然后将读取的字符存储在字符数组s的当前位置。
    // str = s;
    char c = 0;
    while((c = getchar()) != '\n')
    {
        str += c;//插入？？
    }
    return is;//输出时0转为了ascll码的nul空字符
}
